<?php
class ControllerDelimanPhieutrabao extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		$this->data['list_button'] = array();
		foreach($listchek as $method)
		{
			$this->data['list_button'][$method] = $this->user->hasPermission($this->getRoute(), $method);
		}
		
	 	$this->load->model("deliman/phieutrabao");
		$this->load->model("deliman/phieuphanphoi");
		$this->load->model("deliman/nhanvien");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/loaibao");
		$this->load->model("deliman/kybao");
		$this->load->model("common/control");
		$this->data['loaibaos'] = $this->model_deliman_loaibao->getList();
		$this->data['nhanviens'] = $this->model_deliman_nhanvien->getList();
		$this->data['khuvucs'] = array();
		$this->model_deliman_khuvuc->getTree(0, $this->data['khuvucs']);
   	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{
    	$this->getForm();
	}
	
	public function update()
	{			
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';
	
		$this->getForm();	
  	}
	
	public function delete() 
	{
		$listphieutrabaoid=$this->request->post['delete'];
		if(count($listphieutrabaoid))
		{
			foreach($listphieutrabaoid as $phieutrabaoid)
			{
				$this->model_deliman_phieutrabao->delete($phieutrabaoid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('deliman/phieutrabao/insert');
		$this->data['delete'] = $this->url->http('deliman/phieutrabao/delete');		
	
		$data = $this->request->get;
		$where = " AND ( nhanvientraid = '".$this->user->getNhanVien()."' OR  nhanviennhanid = '".$this->user->getNhanVien()."')";
		if (trim($data['tungay']) != "")
		{
			$where .= " AND ngaylap >= '".$this->date->formatViewDate($data['tungay'])."'";	
			
		}
		if (trim($data['denngay']) != "")
		{
			$where .= " AND ngaylap < '".$this->date->formatViewDate($data['denngay'])." 24:00:00'";	
		}
		if($data['nhanvientraid'] != "")
		{
			$where .= " AND nhanvientraid = '".$data['nhanvientraid']."'";
		}
		if($data['nhanviennhanid'] != "")
		{
			$where .= " AND nhanviennhanid = '".$data['nhanviennhanid']."'";
		}
		
		if($data['khuvuctraid'] != "")
		{
			$where .= " AND khuvuctraid = '".$data['khuvuctraid']."'";
		}
		if($data['khuvucnhanid'] != "")
		{
			$where .= " AND khuvucnhanid = '".$data['khuvucnhanid']."'";
		}
		
		if($data['loaibaoid'] != "")
		{
			$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
		}
		if($data['kybaoid'] != "")
		{
			$where .= " AND kybaoid = '".$data['kybaoid']."'";
		}
		$where .= "ORDER BY  `ngaylap` DESC ";
	
		$this->data['datas'] = array();	
		
		$rows = $this->model_deliman_phieutrabao->getChiTietPhieuTraBaoList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/phieutrabao/update&phieutrabaoid='.$this->data['datas'][$i]['phieutrabaoid']);
			$this->data['datas'][$i]['text_edit'] = "Edit";
			
			$this->data['datas'][$i]['link_phanphoibao'] = $this->url->http('deliman/phieutrabao/phanphoibao&phieutrabaoid='.$this->data['datas'][$i]['phieutrabaoid']);
			$this->data['datas'][$i]['text_phanphoibao'] = "Phân phối báo";
			
			$phieutrabao = $this->model_deliman_phieutrabao->getItem($this->data['datas'][$i]['phieutrabaoid']);
			$this->data['datas'][$i]['ngaylap'] = $phieutrabao['ngaylap'];
			$this->data['datas'][$i]['maphieutrabao'] = $phieutrabao['maphieutrabao'];
			$this->data['datas'][$i]['tennhanvienquanly'] = $phieutrabao['tennhanvienquanly'];
			$this->data['datas'][$i]['tennhanviengiaobao'] = $phieutrabao['tennhanviengiaobao'];
			
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		
		
		
		$this->id='content';
		$this->template="deliman/phieutrabao_list.tpl";
		//$this->layout="layout/deliman";
		if($this->request->get['print'] == true)
		{
			$this->layout="layout/print";
		}
		else
		{
			$this->layout="layout/deliman";
		}
		
		$this->render();
	}
	
	public function getKyBaoMoiNhat()
	{
		$this->data['kybaomoinhats'] = $this->model_deliman_kybao->getCacKyBaoMoiNhat();
		foreach($this->data['kybaomoinhats'] as $key => $item)
		{
			$loaibao = $this->model_deliman_loaibao->getItem($item['loaibaoid']);
			$this->data['kybaomoinhats'][$key]['tenloaibao'] = $loaibao['tenloaibao'];
			$this->data['kybaomoinhats'][$key]['soluongcon'] = $this->tinhTongKyBaoChiTieu($item['kybaoid']) - $this->tinhTongKyBaoDaGiaoBao($item['kybaoid']);
		}
	}
	
	public function tinhTongKyBaoChiTieu($kybaoid)
	{
		$nhanvienid = $this->user->getNhanVien();
		//Lay cac khu vuc phu trach
		$where = " AND nhanvienphutrachid = '".$nhanvienid."'";
		$data_khuvucphutrach = $this->model_deliman_khuvuc->getList($where);
		//Lay chi tiet phieu phan phoi theo khu vuc
		$arr_khuvucid = $this->string->matrixToArray($data_khuvucphutrach,'khuvucid');
		
		$where = " AND kybaoid = '".$kybaoid."'";
		$where .= " AND khuvucid in ('". implode("','",$arr_khuvucid) ."')";
		$data_ctphieuphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
		
		$sum = 0;
		foreach($data_ctphieuphanphoi as $item)
		{
			$sum += $item['chitieu'];	
		}
		
		return $sum;
	}
	
	
	
	private function getForm()
	{		
		$ctphanphoi = array();
		$this->data['data_ctphieutrabao'] = array();
		if ((isset($this->request->get['phieutrabaoid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_phieutrabao->getItem($this->request->get['phieutrabaoid']);
			$where = " AND phieutrabaoid = '".$this->data['item']['phieutrabaoid']."'";
			$this->data['data_ctphieutrabao'] = $this->model_deliman_phieutrabao->getChiTietPhieuTraBaoList($where);
			/*$arr_loaibaos = $this->string->matrixToArray($this->data['data_ctphieuphanphoi'],'loaibaoid');
			$where = " AND loaibaoid in ('". implode("','",$arr_loaibaos) ."')";
			$this->data['data_loaibaos'] = $this->model_deliman_loaibao->getList($where);*/
			
			/*foreach($this->data['data_ctphieuphanphoi'] as $key => $item)
			{
				$tongdagiao = $this->tinhTongKyBaoDaGiaoBao($item['kybaoid']);
				$tonchitieu = $this->tinhTongKyBaoChiTieu($item['kybaoid']);
				$this->data['data_ctphieuphanphoi'][$key]['tongdagiao'] = $tongdagiao - $item['soluong'];
				$this->data['data_ctphieuphanphoi'][$key]['soluongton'] = $tonchitieu - $tongdagiao;
				$this->data['data_ctphieuphanphoi'][$key]['tongchitieu'] = $tonchitieu;
			}*/
			
    	}
		else
		{
			$this->data['item']['nhanvientraid'] = $this->user->getNhanVien();
			$this->data['item']['ngaylap'] = $this->date->getToday();
			$this->data['item']['ngaytrabao'] = $this->date->getToday();
			
			
		}
		
		
		$this->id='content';
		$this->template='deliman/phieutrabao_form.tpl';
		$this->layout="layout/deliman";
		$this->render();
	}
	
	
	
	public function save()
	{
		$data = $this->request->post;
		
		if($this->validateForm($data))
		{
			$mon = $this->date->numberFormate($this->date->now['mon']);
			$year = $this->date->now['year'];
			$prefix = $mon.$year;
			
			if($data['maphieutrabao']=="")
				$data['prefix'] ="PTB".$prefix;
			
			$data['ngaylap'] = $this->date->formatViewDate($data['ngaylap']);
			$data['ngaytrabao'] = $this->date->formatViewDate($data['ngaytrabao']);
			$nhanvien = $this->model_deliman_nhanvien->getItem($data['nhanvientraid']);
			$data['masonhanvientra'] = $nhanvien['masonhanvien'];
			$data['tennhanvientra'] = $nhanvien['hoten'];
			
			$khuvuc = $this->model_deliman_khuvuc->getItem($data['khuvuctraid']);
			$data['tenkhuvuctra'] = $khuvuc['tenkhuvuc'];
			
			$nhanvien = $this->model_deliman_nhanvien->getItem($data['nhanviennhanid']);
			$data['masonhanviennhan'] = $nhanvien['masonhanvien'];
			$data['tennhanviennhan'] = $nhanvien['hoten'];
			
			$khuvuc = $this->model_deliman_khuvuc->getItem($data['khuvucnhanid']);
			$data['tenkhuvucnhan'] = $khuvuc['tenkhuvuc'];
			
			if($data['phieutrabaoid']=="")
			{
				$data['phieutrabaoid'] = $this->model_deliman_phieutrabao->insert($data);	
			}
			else
			{
				$this->model_deliman_phieutrabao->update($data);	
			}
			//Xoa chi tiet giao bao
			$listdel = split(",", trim($data['deltrabao'],","));
			
			if(count($listdel))
			{
				foreach($listdel as $id)
				{
					$this->model_deliman_phieutrabao->deleteChiTietPhieuTraBao($id);	
				}
			}
			//Luu chi tiet giao bao
			$arrsoluongtra = $data['soluongtra'];
			
			foreach($arrsoluongtra as $key => $soluongtra)
			{
				$cttrabao['id'] = $data['id'][$key];
				$cttrabao['phieutrabaoid'] = $data['phieutrabaoid'];
				$cttrabao['nhanvientraid'] = $data['nhanvientraid'];
				$cttrabao['nhanviennhanid'] = $data['nhanviennhanid'];
				$cttrabao['khuvuctraid'] = $data['khuvuctraid'];
				$cttrabao['khuvucnhanid'] = $data['khuvucnhanid'];
				$cttrabao['ngaylap'] = $data['ngaylap'];
				$cttrabao['nhanviennhanid'] = $data['nhanviennhanid'];
				$cttrabao['kybaoid'] = $data['kybaoid'][$key];
				$kybao = $this->model_deliman_kybao->getItem($cttrabao['kybaoid']);
				$cttrabao['sokybao'] = $kybao['sokybao'];
				$cttrabao['tenkybao'] = $kybao['tenkybao'];
				$cttrabao['loaibaoid'] = $data['loaibaoid'][$key];
				$loaibao = $this->model_deliman_loaibao->getItem($cttrabao['loaibaoid']);
				$cttrabao['tenloaibao'] = $loaibao['tenloaibao'];
				$cttrabao['ngaytrabao'] = $data['ngaytrabao'];
				$cttrabao['soluongtra'] = $soluongtra;
				$cttrabao['lydotra'] = $data['lydotra'][$key];
				
				$this->model_deliman_phieutrabao->saveChiTietPhieuTraBao($cttrabao);
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		/*if (trim($data['maphieutrabao']) == "") 
		{
      		$this->error['maphieutrabao'] = "Bạn chưa nhập mã chức năng";
    	}
		else
		{
			$where = " AND maphieutrabao = '".$data['maphieutrabao']."'";
			$kybaos = $this->model_deliman_phieutrabao->getList($where);
			if(count($kybaos) > 0 && $data['phieutrabaoid'] == "")
			{
				$this->error['maphieutrabao'] = "Mã chức năng đã tồn tại trong hệ thống";	
			}
		}*/
		
		if (trim($data['maphieutrabao']) != "") 
		{
			$where = " AND maphieutrabao = '".$data['maphieutrabao']."'";
			$kybaos = $this->model_deliman_phieutrabao->getList($where);
			if(count($kybaos) > 0 && $data['phieutrabaoid'] == "")
			{
				$this->error['maphieutrabao'] = "Mã phiếu giao báo đã tồn tại trong hệ thống";	
			}
		}
		if (trim($data['ngaylap']) == "") 
		{
      		$this->error['ngaylap'] = "Bạn chưa chọn ngày lập";
    	}
		if (trim($data['khuvuctraid']) == 0) 
		{
      		$this->error['khuvuctraid'] = "Bạn chưa chọn khu vực trả";
    	}
		
		if (trim($data['nhanvientraid']) == 0) 
		{
      		$this->error['nhanvientraid'] = "Bạn chưa chọn nhân viên trả";
    	}
		
		if(count($data['soluongtra'])==0)
			$this->error['soluongtra'] = "Bạn chưa nhập số lượng báo trả";
		else
		{
			foreach($data['soluongtra'] as $soluongtra)
			{
				if($soluongtra == 0)
					$this->error['soluongtra'] = "Số lượng trả phải lơn hơn 0";
			}
		}
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	public function getKhuVucInfor()
	{
		$khuvucid = $this->request->get['khuvucid'];
		$khuvuc = $this->model_deliman_khuvuc->getItem($khuvucid);
		
		$nhanvienphutrach = $this->model_deliman_nhanvien->getItem($khuvuc['nhanvienphutrachid']);
		$datas['nhanviennhan'] = $nhanvienphutrach;
		
		//Lay nhung nhan vien phat hanh bao tai khu vuc do
		$where = " AND khuvucid = '".$khuvucid."' 
					AND nhanvienphutrachid <> '".$khuvuc['nhanvienphutrachid']."'
					AND tinhtrang <> 'deleted'
					";
		$nhanvienkhuvucs = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where);
		$arr_nhanvienid = $this->string->matrixToArray($nhanvienkhuvucs,'nhanvienphutrachid');

		//Lay nhung nhan vien quan ly khu vuc cap con
		$data_khuvuccon = $this->model_deliman_khuvuc->getChild($khuvucid);
		$arr=$this->string->matrixToArray($data_khuvuccon,'nhanvienphutrachid');
		$arr_nhanvienid = array_merge($arr_nhanvienid,$arr);
		$where = " AND nhanvienid in ('".implode("','",$arr_nhanvienid)."')";
		$datas['nhanvientras'] = $this->model_deliman_nhanvien->getList($where);
		
		
		
		$this->data['output'] = json_encode($datas);
		$this->id="khuvucinfor";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function getKhuVucPhuTrach()
	{
		$nhanvienid = $this->request->get['nhanvienid'];
		$where = " AND nhanvienphutrachid = '".$nhanvienid."' AND tinhtrang <> 'deleted'";
		$data_phutrachkhuvuc = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where);
		
		foreach($data_phutrachkhuvuc as $key => $item)
		{
			$khuvuc = $this->model_deliman_khuvuc->getItem($item['khuvucid']);
			$data_phutrachkhuvuc[$key]['tenkhuvuc'] = $khuvuc['tenkhuvuc']." ".$khuvuc['tenphuongxa']." ".$khuvuc['tenquanhuyen']." ".$khuvuc['tentinhthanh'];
		}
		
		$this->data['output'] = json_encode($data_phutrachkhuvuc);
		$this->id="khuvucinfor";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function getTinhHinhGiaoBao()
	{
		$nhavienid = $this->request->get['nhanvienid'];
		//Lay thong tin giao bao
		$where = " AND nhanviennhanid = '".$nhavienid."'";
		$data_cttrabao = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
		//Lay thong phan phoi bao
		$where = " AND qlkhuvucid = '".$nhavienid."'";
		$data_ctphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
		//Lay thong tin giao bao cho khach hang
		$where = " AND nhanviengiaoid = '".$nhavienid."' AND trangthai = 'dagiao'";
		$data_thongtindatbao = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
		//Lay thong tin tra bao tu cap con
		$where = " AND nhanviengiaoid = '".$nhavienid."' AND trangthai = 'dagiao'";
		$data_thongtindatbao = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
		
	}
}
?>